package cn.oms.auth.provider.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // 设置Security全部放行,因为我们Oauth2的方式验证
    // 原有的session验证就不需要了

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable() //防跨域攻击
                .authorizeRequests()
                .anyRequest().permitAll() //任何请求,全部放行
                .and().formLogin() //表单提交登录
//                .loginPage("login.html")
                ;

        // 所有请求都需要认证，必须登录之后才可以访问
//        http.authorizeRequests().anyRequest().authenticated();
    }

    //当前是配置类,我们框架中需要进行加密,我们注入一个对象到Spring容器
    // 确定它的加密规则,这里仍然使用bcrypt
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    // 当前类继承了一个父类,这个父类中有一个对象包含着验证登录的代码
    // 当前Spring容器需要这个对象,把他保存到Spring容器中
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return  super.authenticationManagerBean();
    }


}
